
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  gui;

import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import utility.CapitalChar;
/**
 * This class generates a StringBuffer of html tags
 */
public class GetReportData
{
	Hashtable<Object, LinkedList<Object>> ObjProp;
	private static char Spchar = '\t';
	private String objectname;
	/**
	 * Constructor sets objectname String ,ObjProp Hashtable and SpChar character
	 * @param ObjectName object name of tag object for which the html tags are to
	 * be generated 
	 * @param ObjProp List of properties of object
	 * @param Spchar Special character 
	 */
	public GetReportData(String ObjectName,Hashtable<Object,LinkedList<Object>> ObjProp,char Spchar) 
	{
		this.objectname = ObjectName;
		this.ObjProp = ObjProp;
		GetReportData.Spchar = Spchar;
	}
	
	//this method generates a file with html code for objects and its properties
	@SuppressWarnings("unchecked")
	/**
	 * This method returns a String containing html tag sequence
	 * 
	 */
	public String getData()
	{
		StringBuffer sbr = new StringBuffer();
				
		try
		{
			System.out.println("The object name is "+objectname);			
			
			LinkedList<Object> prop = (LinkedList<Object>)ObjProp.get(objectname);
			sbr.append(this.generateHtmlTags(prop));
			System.out.println("The sbr "+sbr.toString());
			
			return sbr.toString();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * This method generates html tags for a property list and returns a String.
	 * @param prop List of properties
	 * @return String containing html tags
	 */
	public String generateHtmlTags(LinkedList<Object> prop)
	{
		StringBuffer sbr = new StringBuffer();
		int size = prop.size();
		guiproperty.HTMLInterface obj = null;
		StringBuffer buffer = new StringBuffer();
		String tagtype = " ";
		String displaytext = " ";
		boolean flag = false;
		
		try 
		{			
			for(int i=0;i<size;i++)
			{				
				String element = (String)prop.element();
				System.out.println("The element "+element+" is now removed");
				prop.remove();
				String [] property_arr = element.split(String.valueOf(Spchar));
				
				String property = property_arr[0].trim();
				System.out.println("The property is "+property);
				String property_value = property_arr[1].trim();
				System.out.println("The property val is "+property_value);
				//String property_order  = property_arr[2].trim();
				
				try 
				{					
					String classname = CapitalChar.makeFirstCharCapital(property);
					System.out.println("The class name is "+classname);
					Class myclass = Class.forName("guiproperty."+classname);
					
					obj = (guiproperty.HTMLInterface)myclass.newInstance();
                                    	obj.setObjectName(property_value);
					obj.setObjPropertyList(ObjProp);
					//obj.setDisplayText(text);
					buffer.append(obj.getHTMLCode());					
				} 
				catch (ClassNotFoundException e) 
				{
					//e.printStackTrace();
					System.out.println("There is no class "+property);
					if(property.toLowerCase().equals("tagtype"))
					{
						tagtype = property_value;
						flag = true; 
					}
					if(property.toLowerCase().equals("displaytext"))
					{
						displaytext = property_value;
					}
				}
			}
			if(flag)
			{
			sbr.append("<"+tagtype+">");
			sbr.append(displaytext);
			sbr.append(buffer.toString());
			sbr.append("</"+tagtype+">");
			}
			else
			{
				sbr.append(buffer.toString());
			}
			System.out.println(sbr.toString());
			
			return sbr.toString();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			return " " ;
		}
				
	}
			
}
